﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VAdvantage.Utility;
using VAdvantage.Classes;

namespace VAdvantage.Framework
{
    public partial class Editor : ChildWindow
    {
        private Control objCtrl;
        //private DataGridViewCell objCell;
        private bool isGrid = false;

        private bool readOnly = false;

        private string _ctrlText;

        private String _text = "";

        public string EditorText
        {
            get
            {
                return this.rtxtTextTab.Text;
            }
            //set
            //{ 
            //_ctrlText=
            //}
        }

        public Editor()
        {
            InitializeComponent();
            LayoutRoot.Background = new SolidColorBrush(DataBase.GlobalVariable.WINDOW_BACK_COLOR);
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {

            if (!isGrid && objCtrl != null && ((VAdvantage.Controls.IControl)objCtrl).GetDisplayType() == DisplayType.String)
            {
                rtxtTextTab.Text = rtxtTextTab.Text.Replace("\n", " ");
                //replace tab in the text
                rtxtTextTab.Text = rtxtTextTab.Text.Replace("\t", "  ");
                //set the cureent written text into the variable
            }
            if (!isGrid)
            {
                //if (objCtrl != null)
                //{
                //    objCtrl.Text = rtxtTextTab.Text.Trim();
                //}
                _text = rtxtTextTab.Text.Trim();
            }
            else
            {
                _text = rtxtTextTab.Text.Trim();
            }

            //close the current window
           

            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        public Editor(string formName, string totalText, string showText, Control ctrl):this()
        {
            this.Title = formName;
            this.lblTotal.Text = totalText;//show text
            lblCurrentText.Text = showText.Length.ToString();//current text
            lblNoOfText.Text = showText.Length.ToString();//whole text in control
            //Store Referenceweb
            objCtrl = ctrl;
            //show/write text in main text box
            this.rtxtTextTab.Text = showText;
            //count charactor
            CountText();
        }



        /// <summary>
        /// Count text function in the control
        /// </summary>
        private void CountText()
        {
            //total number of charactor
            lblNoOfText.Text = rtxtTextTab.Text.Length.ToString();
            //current text length
            lblCurrentText.Text = rtxtTextTab.Text.Length.ToString();
            //set text range comparison
            try
            {
                if (int.Parse(lblNoOfText.Text) > int.Parse(lblTotal.Text))
                {
                    lblOprator.Text = ">";
                    //if text length grator then the set length turn text into red
                    SetColor(Colors.Red);
                }
                else
                {
                    lblOprator.Text = "<";
                    //else black
                    SetColor(Colors.Black);
                }
            }
            catch (Exception ex)
            {

            }
            //set the location
            //lblOprator.Location = new Point(lblCurrentText.Right - 2, lblCurrentText.Top);
            //lblTotal.Location = new Point(lblOprator.Right - 2, lblCurrentText.Top);

            //panel1.Width = this.Width - lblNoOfText.Width;
        }

        /// <summary>
        /// set color for count text
        /// </summary>
        /// <param name="clr">get control</param>
        /// 
        SolidColorBrush brush = new SolidColorBrush();
        private void SetColor(Color clr)
        {
            brush.Color  = clr;
            lblCurrentText.Foreground = brush;//forecolor of text
            lblTotal.Foreground = brush;//label control color
            lblOprator.Foreground = brush;//fore color
        }


        public static void StartEditor(string header, string text, bool editable, int maxSize, Control ctrl)
        {
            string hdr = header;
            if (hdr == null || hdr.Length == 0)
                hdr = Msg.GetMsg(Envs.GetCtx(), "Editor", true);
            //	Start it
            Editor ed = new Editor(hdr, text, text, ctrl);
            //Editor ed = new Editor();
            ed.Show();
        }

        private void rtxtTextTab_KeyDown(object sender, KeyEventArgs e)
        {
            CountText();
        }

        private void rtxtTextTab_KeyUp(object sender, KeyEventArgs e)
        {
            CountText();
        }

        /// <summary>
        ///	Get Text
        /// </summary>
        /// <returns></returns>
        public String GetText()
        {
            return _text;
        }

        private void tabControl1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (tabControl1 == null)
                return;
            if (tabControl1.SelectedIndex == 1)
            {
                browser.NavigateToString(rtxtTextTab.Text );

            }
        }	//
    }
}

